package cn.iocoder.yudao.module.student.controller.app.studentEntryInfo;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.api.praxisProject.PraxisProjectApi;
import cn.iocoder.yudao.module.member.api.praxisProject.dto.PraxisAnnexBaseDTO;
import cn.iocoder.yudao.module.member.api.praxisProject.dto.PraxisProjectRespDTO;
import cn.iocoder.yudao.module.student.controller.app.studentEntryInfo.vo.*;
import cn.iocoder.yudao.module.student.controller.app.studentProject.vo.StudentAnnexVO;
import cn.iocoder.yudao.module.student.service.studentEntryInfo.StudentEntryInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -学生端- 项目报名信息")
@RestController
@RequestMapping("/student/entry")
@Validated
public class StudentEntryInfoController {

    @Resource
    private StudentEntryInfoService studentEntryInfoService;
    @Resource
    private PraxisProjectApi praxisProjectApi;

    @PostMapping("/create")
    @ApiOperation("创建学生项目报名信息")
    @PreAuthenticated
    public CommonResult<Boolean> createPraxisEntryInfo(@Valid @RequestBody StudentEntryInfoBaseVO createReqVO) {
        createReqVO.setScheduleType(1);
        Assert.notNull(createReqVO.getStudentId(),"报名学生不能为空");
        PraxisProjectRespDTO project = praxisProjectApi.getPraxisProjectInfoById(createReqVO.getProjectId(),1);
        if(null==project){
            return error("该项目不存在或已下架");
        }
        if(project.getStartTime().after(new Date())){
            return error("项目还未开始");
        }
        if(project.getEndTime().before(new Date())){
            return error("项目已结束");
        }
        if(1!=project.getStatus()){
            return error("项目未发布或已下架");
        }
        if(StringUtils.isBlank(project.getExamineStatus()) || !"1".equals(project.getExamineStatus())){
            return error("项目审核未通过");
        }
        if(project.getItemPeopleNum()<createReqVO.getStuList().size()){
            return error("项目允许最大报名人数为"+project.getItemPeopleNum()+"人");
        }
        studentEntryInfoService.createPraxisEntryInfo(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @ApiOperation("更新项目报名信息")
    @PreAuthenticated
    public CommonResult<Boolean> updatePraxisEntryInfo(@Valid @RequestBody StudentEntryInfoBaseVO updateReqVO) {
        Assert.notNull(updateReqVO.getStudentId(),"报名学生不能为空");
        Assert.notNull(updateReqVO.getId(),"ID不能为空");
        PraxisProjectRespDTO project = praxisProjectApi.getPraxisProjectInfoById(updateReqVO.getProjectId(),1);
        if(null==project){
            return error("该项目不存在或已下架");
        }
        if(project.getStartTime().after(new Date())){
            return error("项目还未开始");
        }
        if(project.getEndTime().before(new Date())){
            return error("项目已结束");
        }
        if(1!=project.getStatus()){
            return error("项目未发布或已下架");
        }
        if(project.getItemPeopleNum()<updateReqVO.getStuList().size()){
            return error("项目允许最大报名人数为"+project.getItemPeopleNum()+"人");
        }
        studentEntryInfoService.updatePraxisEntryInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除项目报名信息")
    @PreAuthenticated
    @ApiImplicitParam(name = "id", value = "报名ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePraxisEntryInfo(@RequestParam("id") Long id) {
        studentEntryInfoService.deletePraxisEntryInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目报名信息")
    @PreAuthenticated
    @ApiImplicitParam(name = "id", value = "报名ID", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<StudentEntryInfoRespVO> getPraxisEntryInfo(@RequestParam("id") Long id) {
        StudentEntryInfoRespVO praxisEntryInfo = studentEntryInfoService.getPraxisEntryInfo(id);
        if(null!=praxisEntryInfo){
            PraxisProjectRespDTO  projectInfo = praxisProjectApi.getPraxisProjectInfoById(praxisEntryInfo.getProjectId(),1);
            List<PraxisAnnexBaseDTO> annexList = praxisProjectApi.getPraxisAnnexList(projectInfo.getId(), 1);
            List<StudentAnnexVO> aList=new ArrayList<>();
            if(CollectionUtils.isNotEmpty(annexList)){
                annexList.stream().forEach(m->{
                    StudentAnnexVO b=new StudentAnnexVO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
                });
                praxisEntryInfo.setAnnexVOList(aList);
            }
            praxisEntryInfo.setAnnexDownType(projectInfo.getAnnexDownType());
            praxisEntryInfo.setClosingDate(projectInfo.getClosingDate());
            praxisEntryInfo.setStartTime(projectInfo.getStartTime());
            praxisEntryInfo.setEndTime(projectInfo.getEndTime());
            praxisEntryInfo.setTitle(projectInfo.getTitle());
        }
        return success( praxisEntryInfo);
    }


    @PostMapping("/page")
    @PreAuthenticated
    @ApiOperation("获得项目报名信息分页")
    public CommonResult<PageResult<StudentEntryInfoRespVO>> getPraxisEntryInfoPage(@Valid @RequestBody StudentEntryInfoPageReqVO pageVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        pageVO.setStudentId(loginUserId);
        pageVO.setScheduleType(1);
        PageResult<StudentEntryInfoRespVO> pageResult = studentEntryInfoService.getPraxisEntryInfoPage(pageVO);
        return success(pageResult);
    }

    @PutMapping("/uploadAnnex")
    @ApiOperation("上传报名材料")
    @PreAuthenticated
    public CommonResult<Boolean> uploadAnnex(@Valid @RequestBody StudentEntryInfoUpdateVO updateReqVO) {
        studentEntryInfoService.uploadAnnex(updateReqVO);
        return success(true);
    }

    @PostMapping("/schedulePage")
    @PreAuthenticated
    @ApiOperation("获得比赛分页")
    public CommonResult<PageResult<StudentEntryInfoRespVO>> entryInfoPage(@Valid @RequestBody StudentEntryInfoPageReqVO pageVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if(null==loginUserId){
            loginUserId= WebFrameworkUtils.getLoginUserId();
        }
        if(null==loginUserId){
            return success(new PageResult<>(null, 0L));
        }
        pageVO.setStudentId(loginUserId);
        PageResult<StudentEntryInfoRespVO> pageResult = studentEntryInfoService.getSchedulePage(pageVO);
        return success(pageResult);
    }


    @GetMapping("/getResultInfo")
    @PreAuthenticated
    @ApiOperation("查看结果信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "报名ID", required = true  , dataTypeClass = Long.class),
            @ApiImplicitParam(name = "scheduleType", value = "类型;1:遴选;2:初赛;3:复赛;4:决赛;", required = true  , dataTypeClass = Integer.class)
    })
    public CommonResult<StudentEntryChosenVO> getResultInfo(@RequestParam("id") Long id, @RequestParam("scheduleType") Integer scheduleType) {
        StudentEntryChosenVO praxisEntryInfo = studentEntryInfoService.getResultInfo(id);
        if(null!=praxisEntryInfo && scheduleType!=4){
            PraxisProjectRespDTO  projectInfo = praxisProjectApi.getPraxisProjectInfoById(praxisEntryInfo.getProjectId(),scheduleType);
            praxisEntryInfo.setClosingDate(projectInfo.getClosingDate());
            praxisEntryInfo.setStartTime(projectInfo.getStartTime());
            praxisEntryInfo.setEndTime(projectInfo.getEndTime());
            praxisEntryInfo.setTitle(projectInfo.getTitle());
            praxisEntryInfo.setCompanyName(projectInfo.getCompanyName());
        }
        return success( praxisEntryInfo);
    }


    @GetMapping("/getResultInfoByProjectId")
    @PreAuthenticated
    @ApiOperation("根据项目ID查看结果信息")
    @ApiImplicitParam(name = "projectId", value = "项目ID", required = true  , dataTypeClass = Long.class)
    public CommonResult<StudentResultVO> getResultInfoByProjectId(@RequestParam("projectId") Long projectId  ) {
        StudentResultVO resultVO = studentEntryInfoService.getResultInfoByProjectId(projectId);
        if(null!=resultVO){
            List<PraxisAnnexBaseDTO> annexList = praxisProjectApi.getPraxisAnnexList(resultVO.getId(), 2);
            List<StudentAnnexVO> aList=new ArrayList<>();
            if(CollectionUtils.isNotEmpty(annexList)){
                annexList.stream().forEach(m->{
                    StudentAnnexVO b=new StudentAnnexVO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
                });
                resultVO.setAnnexList(aList);
            }
        }
        return success( resultVO);
    }

    @GetMapping("/getPraxisStudentNum")
    @PreAuthenticated
    @ApiOperation("获取是否已参加项目的比赛")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "studentId", value = "学生ID",required = true,   dataTypeClass = Integer.class)
    })
    public CommonResult<Long> getPraxisStudentNum(@RequestParam("projectId") Long projectId,@RequestParam("studentId") Long studentId) {
        return success(studentEntryInfoService.getPraxisStudentNum(projectId,studentId));
    }

    @GetMapping("/getCheckStatus")
    @PreAuthenticated
    @ApiOperation("查询是否报名已入选")
    @ApiImplicitParam(name = "projectId", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Integer> getCheckStatus(@RequestParam("projectId") Long projectId ) {
        return success(studentEntryInfoService.getCheckStatus(projectId));
    }

}
